/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.NguyenLieu;
import UTIL.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ChanhTin
 */
public class NguyenLieuDAO {
     
    /**
     * @desc lay danh sach nguyen lieu
     * @return 
     */
    public static List<NguyenLieu> layDanhSach() {
        List<NguyenLieu> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select nl from NguyenLieu nl";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;  
    }
    
    /**
     * @desc tim nguyen lieu theo ma nguyen lieu
     * @param maNguyenLieu
     * @return 
     */
    public static NguyenLieu layThongTinNguyenLieu(int maNguyenLieu) {
        NguyenLieu nl = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            nl = (NguyenLieu) session.get(NguyenLieu.class, maNguyenLieu);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return nl;
    }
    
    /**
     * @desc them nguyen lieu
     * @param nl
     * @return 
     */
    public static boolean themNguyenLieu(NguyenLieu nl) throws HibernateException{
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (NguyenLieuDAO.layThongTinNguyenLieu(nl.getMaNguyenLieu())!=null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.save(nl); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag;
    }
    
    /**
     * @desc cap nhat thong tin nguyen lieu
     * @param nl
     * @return 
     */
    public static boolean capNhatThongTinNguyenLieu(NguyenLieu nl) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (NguyenLieuDAO.layThongTinNguyenLieu(nl.getMaNguyenLieu()) == null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.update(nl); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
           //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    } 
    
    /**
     * @desc xoa nguyen lieu
     * @param maNguyenLieu
     * @return 
     */
    public static boolean xoaNguyenLieu(int maNguyenLieu) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        NguyenLieu nl = NguyenLieuDAO.layThongTinNguyenLieu(maNguyenLieu); 
        if(nl==null){ 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.delete(nl); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    }
}
